package com.example.test;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class MainActivity extends AppCompatActivity {
    private Button bt_ConMysql;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt_ConMysql = (Button) findViewById(R.id.bt_ConMysql);
        bt_ConMysql.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println(Connection_Init());
                }
                }).start();
            }
        });
    }
    public Connection Connection_Init()
    {
        Connection connection = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");//加载驱动
            long in1 = System.currentTimeMillis();
            try {
                connection = DriverManager.getConnection(
                        "jdbc:mysql://10.21.151.218:3306/test?useUnicode=true&characterEncoding=utf8",
                        "root",
                        "root");// 输入连接参数
                System.out.println(connection);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            long end = System.currentTimeMillis();
            System.out.println("连接耗时" + (end - in1));//连接耗时
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return connection;
    }

}
